package controllers

import (
	"are-you-hungry-db/models"
	beego "github.com/beego/beego/v2/server/web"
)

type CommentController struct {
	beego.Controller
}

func (c *CommentController) Create() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	data := c.GetString("data")
	user_id, _ := c.GetInt("user")
	order_id, _ := c.GetInt("order")
	var comment models.Comment
	comment.OrderId = order_id
	comment.UserId = user_id
	comment.Data = data
	models.DB.Create(&comment)
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "评论发布成功",
	}
	c.ServeJSON()
}
